D1.並び替え
●●●デ−タ範囲の1セルをアクティブにして、B列をキ−に昇順に並び替えたケ−ス。
Sub Macro1()
Range("C1").Select
Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
:=xlPinYin
End Sub
■
PageTop
D2.フィルタ
PageTop
D2-1.オ−トフィルタ
●●●C列に"K"文字がある行を選択。
Sub Macro1()
Range("C8").Select
Selection.AutoFilter
Selection.AutoFilter Field:=3, Criteria1:="=K", Operator:=xlAnd
End Sub
■
Top
D2-2.すべて表示
●●●全デ−タを表示(矢印はそのまま)
Sub Macro1()
ActiveSheet.ShowAllData
End Sub
Top
D2-3.フィルタ−オプションの設定
●●●下記は「先頭行を見出しとして使用」を設定したケ−ス。
Sub Macro1()
Range("C7").Select
Range("A1:C14").AdvancedFilter Action:=xlFilterInPlace, Unique:=False
End Sub
Top
D3.フォ−ム
●●●ダイアログを表示(検索条件や検索結果等は記録されない)。
Sub Macro1()
Range("A5").Select
ActiveSheet.ShowDataForm
End Sub
■
PageTop
D4.集計
●●●下記はデ−タベ−スを集計したケ−ス(デ−タの入っているセルを選択してから実行)。
Sub Macro1()
'集計
Range("A2").Select
Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End Sub
●●●集計の解除
Sub Macro2()
Range("A1").Select
Selection.RemoveSubtotal
End Sub
PageTop
D5.入力規制
○●●A列の日付入力を1999/4/1〜1999/12/31までに規制したケ−ス。
Sub Macro1()
Columns("A:A").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateDate, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="1999/4/1", Formula2:="1999/12/31"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
Range("A1").Select
End Sub
PageTop
D6.テ−ブル
●●●デ−タ範囲は("A2:B8")で"B2"に数式入れ、("A1")代入セルでテ−ブルを作成したケ−ス。
Sub Macro1()
Range("A2:B8").Select
Selection.Table ColumnInput:=Range("A1")
Range("A9").Select
End Sub
■ 数式を消したらExcelを終了出来なくなった。本機能はExcelに不慣れの方は使用しない方が
良いと思われる。
PageTop
D7.区切り位置
●●●セル("A1:A3")に入っている、カンマ付きのテキストデ−タをExcelデ−タに変換。
Sub Macro1()
Range("A1:A3").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1))
End Sub
PageTop
D8.統合
●●●セル"B2:C8"のデ−タを統合し、"B10"へ貼り付けたケ−ス。
Sub Macro1()
Range("B10").Select
Application.CutCopyMode = False
Selection.Consolidate Sources:="[Book12]Sheet2!R2C2:R8C3", Function:=xlSum _
, TopRow:=False, LeftColumn:=False, CreateLinks:=False
Range("A1").Select
End Sub
PageTop
D9.グル−プとアウトラインの設定
PageTop
D9-1.詳細を表示しない
●●●集計してあるデ−タベ−スのセル("B6")を集計してある部分を非表示にしたケ−ス。
Sub Macro1()
Range("B6").Select
ExecuteExcel4Macro "SHOW.DETAIL(1,10,FALSE,,1)"
End Sub
■ 集計の基準になっている列を選択して本項目を実施すると「不正処理の強制終了」になる
Top
D9-2.詳細デ−タの表示
●●●非表示になっている集計の詳細デ−タの表示する。
Sub Macro1()
Range("B11").Select
ExecuteExcel4Macro "SHOW.DETAIL(1,10,TRUE,,1)"
End Sub
Top
D9-3.グル−プ化
●●●2行〜7行をグル−プ化したケ−ス。
Sub Macro1()
Range("A2:A7").Select
Selection.Rows.Group
Range("F7").Select
End Sub
■
Top
D9-4.グル−プ解除
●●●2行〜7行をグル−プを解除したケ−ス。
Sub Macro1()
Range("A2:A7").Select
Selection.Rows.Ungroup
End Sub
Top
D9-5.アウトラインの自動作成
●●●セル("A2")を選択しアウトラインレベルを設定シタケ−ス。
Sub Macro1()
Range("A2").Select
ActiveSheet.Outline.AutomaticStyles = False
Selection.AutoOutline
End Sub
Top
D9-6.アウトラインのクリア
●●●本項実施でアウトラインがクリアされる(集計したデ−タはそのまま残っている)
Sub Macro1()
Selection.ClearOutline
End Sub
Top
D9-7.設定
●●●集計したデ−タを「詳細デ−タの下」に設定したケ−ス。
Sub Macro1()
With ActiveSheet.Outline
.AutomaticStyles = False
.SummaryRow = xlBelow
.SummaryColumn = xlRight
End With
End Sub
Top
D10.ピポットテ−ブルとピポットグラフレポ−ト
●●●ピポットテ−ブルテ−ブルの作成例。
Sub Macro1()
Range("B4").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"'Sheet1 (2)'!R1C1:R27C4").CreatePivotTable TableDestination:=Range("G2"), _
TableName:="ピボットテーブル5"
ActiveSheet.PivotTables("ピボットテーブル5").SmallGrid = False
ActiveSheet.PivotTables("ピボットテーブル5").AddFields RowFields:="日付", _
ColumnFields:="製品"
ActiveSheet.PivotTables("ピボットテーブル5").PivotFields("数量").Orientation = _
xlDataField
Application.CommandBars("PivotTable").Visible = False
End Sub
データデースの量がよく変更になる場合は、下記のようにセル範囲を自動的に
指定する方法が便利です。
Myrang = Range("A2").CurrentRegion.Address
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=Myrang).CreatePivotTable _
TableDestination:=Range("G2"), TableName:="ピボットテーブル1"
PageTop
D11.外部デ−タの取り込み
PageTop
D11-1.保存したクエリの実行
○●●
Sub Macro1()
'工事中
End Sub
■
Top
D11-2.新しいWebクエリ
○●●rirekiv2.htmlを表として読み込んだケ-ス
Sub Macro1()
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;file:///B:/iryohp/sonota/rirekiv2.html", Destination:=Range("A1"))
.Name = "rirekiv2"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingAll
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.Refresh BackgroundQuery:=False
End With
End Sub
■
Top
D11-3.新しいデ−タベ−スクエリ
○●●
Sub Macro1()
'工事中
End Sub
■
Top
D11-4.テキストファイルのインポ−ト
○○●
Sub Macro1()
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\実績DATA.TXT", _
Destination:=Range("A1"))
.Name = "実績DATA"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.Refresh BackgroundQuery:=False
End With
End Sub
■
Top
D11-5.クエリの編集
○○●Webペ−ジ全体として取り込んだクエリをHTML形式に編集したケ−ス
Sub Macro1()
With Selection.QueryTable
.Connection = "URL;file:///B:/iryohp/index.html"
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingAll
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.Refresh BackgroundQuery:=False
End With
End Sub
■
Top
D11-6.デ−タ範囲プロパティ
○○●下記は標準設定のうち、更新周期のみ60→55に変えた例
Sub Macro1()
With Selection.QueryTable
.Name = "index"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 55
End With
End Sub
■
Top
D11-7.パラメ−タ
○●●
Sub Macro1()
'工事中
End Sub
■
Top
D12.デ−タの更新
○●●
Sub Macro1()
Selection.QueryTable.Refresh BackgroundQuery:=False
End Sub
■
PageTop